package com.haitaoit.chengquan.module.center.activity.info;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.center.model.UserInfoModel;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyMoneyTixianActivity extends BaseActivity {
    @ViewInject(R.id.pay_way1)
    CheckBox pay_way1;
    @ViewInject(R.id.pay_way2)
    CheckBox pay_way2;
    @ViewInject(R.id.pay_way3)
    CheckBox pay_way3;
    @ViewInject(R.id.money_yue)
    TextView money_yue;
    @ViewInject(R.id.tixian_money)
    EditText tixian_money;
    @ViewInject(R.id.tixian_type)
    EditText tixian_type;
    @ViewInject(R.id.tixian_card)
    EditText tixian_card;
    @ViewInject(R.id.tixian_name)
    EditText tixian_name;
    @ViewInject(R.id.remarks)
    EditText remarks;

    @ViewInject(R.id.tixian_phone)
    EditText tixian_phone;
    @ViewInject(R.id.tixian_zfbzh)
    EditText tixian_zfbzh;
    @ViewInject(R.id.tixian_zfbname)
    EditText tixian_zfbname;
    @ViewInject(R.id.tixian_ll1)
    LinearLayout tixian_ll1;
    @ViewInject(R.id.tixian_ll3)
    LinearLayout tixian_ll3;
    boolean isSetText=false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_my_money_tixian);
        setTitle_V("提现");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);

        money_yue.setText("可提现余额：¥ " + UserInfoModel.getUser_info().getBalance());
        tixian_ll3.setVisibility(View.GONE);
        tixian_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                /*if (tixian_money.getText().toString().indexOf(".") >= 0) {
                    if (tixian_money.getText().toString().indexOf(".", tixian_money.getText().toString().indexOf(".") + 1) > 0) {
                        tixian_money.setText(tixian_money.getText().toString().substring(0, tixian_money.getText().toString().length() - 1));
                        tixian_money.setSelection(tixian_money.getText().toString().length());
                    }

                }*/
                if(isSetText){
                   return;
                }
                int pointIndex = s.toString().trim().indexOf(".");
                if (pointIndex >= 0) {
                    String[] split = s.toString().split("\\.");
                    if (split.length > 1 && split[1].length() > 1) {
                        if(TextUtils.isEmpty(split[0])){
                            split[0]="0";
                        }
                        isSetText=true;
                        tixian_money.setText( split[0]+ "." + split[1].substring(0, 2));
                        Editable etext = tixian_money.getText();
                        Selection.setSelection(etext, etext.length());
                        isSetText=false;
                    }
                }
            }
        });
    }

    @OnClick({R.id.pay_way1, R.id.pay_way2, R.id.pay_way3, R.id.tixian_to,})
    public void onClick(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        switch (v.getId()) {
            case R.id.pay_way1:
                pay_way1.setChecked(true);
                pay_way2.setChecked(false);
                pay_way3.setChecked(false);
                tixian_ll1.setVisibility(View.VISIBLE);
                tixian_ll3.setVisibility(View.GONE);
                break;
            case R.id.pay_way2:
                pay_way1.setChecked(false);
                pay_way2.setChecked(true);
                pay_way3.setChecked(false);
                break;
            case R.id.pay_way3:
                pay_way1.setChecked(false);
                pay_way2.setChecked(false);
                pay_way3.setChecked(true);
                tixian_ll1.setVisibility(View.GONE);
                tixian_ll3.setVisibility(View.VISIBLE);
                break;
            case R.id.tixian_to:
                if (tixian_money.getText().toString().equals("")) {
                    ToastUtils.showToast("请输入提现金额！");
                } else if (Float.valueOf(tixian_money.getText().toString()) >
                        Float.valueOf(UserInfoModel.getUser_info().getBalance())) {
                    ToastUtils.showToast("提现金额大于可提现余额！");
                } else if (pay_way1.isChecked() && !Utils.isMobileNum(tixian_phone.getText().toString())) {
                    ToastUtils.showToast("请输入手机号或手机号格式有误！");
                } else if (pay_way1.isChecked() && tixian_zfbzh.getText().toString().equals("")) {
                    ToastUtils.showToast("请输入  支付宝账号！");
                } else if (pay_way1.isChecked() && tixian_zfbname.getText().toString().equals("")) {
                    ToastUtils.showToast("请输入支付宝名称！");
                } else if (pay_way3.isChecked() && tixian_type.getText().toString().equals("")) {
                    ToastUtils.showToast("请输入开户行类型！");
                } else if (pay_way3.isChecked() && tixian_card.getText().toString().equals("")) {
                    ToastUtils.showToast("请输入银行卡卡号！");
                } else if (pay_way3.isChecked() && tixian_name.getText().toString().equals("")) {
                    ToastUtils.showToast("请输入持卡人姓名！");
                } else {
                    httpadd_present_application();
                }
                break;
        }
        super.onClick(v);
    }

    private void httpadd_present_application() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        String sign = GetSign.genPackageSign(list);

        String url = Constant.add_present_application + "user_id=" +
                PreferenceUtils.getPrefString("user_id", "") + "&sign=" + sign;
        JSONObject params = new JSONObject();
        try {
            params.put("money", tixian_money.getText().toString());
            params.put("remarks", remarks.getText().toString());
            if (pay_way1.isChecked()) {
                params.put("type", "1");
                params.put("bank_account", tixian_phone.getText().toString());
                params.put("bank_card", tixian_zfbzh.getText().toString());
                params.put("cardholder", tixian_zfbname.getText().toString());
            } else if (pay_way2.isChecked()) {
                params.put("type", "2");
            } else if (pay_way3.isChecked()) {
                params.put("type", "3");
                params.put("bank_account", tixian_type.getText().toString());
                params.put("bank_card", tixian_card.getText().toString());
                params.put("cardholder", tixian_name.getText().toString());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        HttpUtilsSingle.doPost(this, true, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String result = arg0.result.toString();
                try {
                    JSONObject json = new JSONObject(result);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            break;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            double tiXianMoney = Double.parseDouble(tixian_money.getText().toString());
                            Intent intent=new Intent();
                            intent.putExtra("money",tiXianMoney);
                            setResult(101,intent);
                            finish();
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
